﻿Public Class Settings
    Private _settings As Dictionary(Of String, String)
    Private _file As String

    Public Sub New()
        _settings = New Dictionary(Of String, String)

    End Sub

    Public Sub New(ByVal File As IO.FileInfo)
        _settings = New Dictionary(Of String, String)
        _file = File.FullName

        Dim xDoc As XDocument = XDocument.Load(File.FullName)
        Dim data = From item In xDoc...<Settings>.<Setting> _
                   Select item.@Key, item.Value _
                   Order By Key Ascending

        For Each item In data
            _settings.Add(item.Key, item.Value)
        Next
    End Sub

    Public Sub Save()
        Dim data = <?xml version="1.0" encoding="utf-8"?>
                   <Settings>
                       <%= From item In _settings Select
                           <Setting Key=<%= item.Key %>><%= item.Value %></Setting>
                       %>
                   </Settings>

        Dim sw As New IO.StreamWriter(_file)
        sw.Write(data.ToString)
        sw.Close()
    End Sub

    Public Shadows ReadOnly Property ToString As String
        Get
            Dim data = <?xml version="1.0" encoding="utf-8"?>
                       <Settings>
                           <%= From item In _settings Select
                               <Setting Key=<%= item.Key %>><%= item.Value %></Setting>
                           %>
                       </Settings>

            Return data.ToString
        End Get
    End Property

    Public ReadOnly Property Data
        Get
            Dim _data = <?xml version="1.0" encoding="utf-8"?>
                        <Settings>
                            <%= From item In _settings Select
                                <Setting Key=<%= item.Key %>><%= item.Value %></Setting>
                            %>
                        </Settings>
            Return _data
        End Get
    End Property

    Public ReadOnly Property Items As Dictionary(Of String, String)
        Get
            Return _settings
        End Get
    End Property


End Class
